package com.sunrise.controller;

import com.redislabs.redisgraph.Record;
import com.redislabs.redisgraph.ResultSet;
import com.redislabs.redisgraph.graph_entities.Edge;
import com.redislabs.redisgraph.graph_entities.Node;
import com.redislabs.redisgraph.graph_entities.Property;
import com.redislabs.redisgraph.impl.api.RedisGraph;
import com.sunrise.vo.system.graph.NodeEntityVO;
import com.sunrise.vo.system.graph.RelationshipEntityVO;
import com.sunrise.vo.system.graph.WrapperResponseVO;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import result.GraceJSONResult;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @Author: liansen
 * @Date: 2024-01-06  23:36
 */
@RestController
@RequestMapping("/system/graph")
public class GraphController {

    private static final String HOST = "117.72.45.208";
    private static  final int PORT = 6380;

    @GetMapping("/getGraphData")
    public GraceJSONResult getGraphData() {
        //连接redisgraph
        RedisGraph graph = new RedisGraph(HOST, PORT);
        //ResultSet result =
        //        graph.query("testgraph", "MATCH (a:Person{name:'liansen'})-[r:friends]->(b:Person{name:'Bob'}) RETURN a, r, b");

        ResultSet result =
                graph.query("testgraph", "MATCH (a:Person{name:'bddriver'})-[r:friends]->(b:Person{name:'szdriver'}) RETURN a, r, b");

        //List<GraphVO> graphVOList = new ArrayList<>();
        List<NodeEntityVO> nodes = new ArrayList<>();
        List<RelationshipEntityVO> relationships = new ArrayList<>();

        WrapperResponseVO response = null;
        while (result.hasNext()) {
            Record record = result.next();
            for (int i = 0; i < record.size(); i++) {

                Object value = record.getValue(i);

                if (value instanceof Node) {
                    Node node = (Node) value;
                    NodeEntityVO nodeEntity = new NodeEntityVO();
                    nodeEntity.setId(node.getId());
                    nodeEntity.setLabels(node.getLabel(0));
                    Property properties = node.getProperty("name");
                    Property property = node.getProperty("sign");
                    HashMap<String, Object> map = new HashMap<>();
                    map.put(properties.getName(), properties.getValue());
                    map.put(property.getName(), property.getValue());
                    nodeEntity.setProperties(map);
                    nodes.add(nodeEntity);
                } else if (value instanceof Edge) {
                    Edge valueEdge = (Edge) value;
                    RelationshipEntityVO relationshipEntityVO = new RelationshipEntityVO();
                    relationshipEntityVO.setId(valueEdge.getId());
                    relationshipEntityVO.setType(valueEdge.getRelationshipType());
                    relationships.add(relationshipEntityVO);
                }
            }
            response = new WrapperResponseVO();
            response.setNodes(nodes);
            response.setRelationships(relationships);
        }
        return GraceJSONResult.ok(response);
    }
}
